/*
 * @Author: gavindon
 * @LastEditors: gavindon 530893850@qq.com
 * @Date: 2023-04-12 10:54:53
 * @LastEditTime: 2023-04-14 09:16:10
 * @Description: 
 */
import axios, { InternalAxiosRequestConfig, AxiosResponse, AxiosError } from "axios"
import { AxiosInstance } from "axios";
import ResponseError from "./ResponseError";

//创建axios实例
const fuel: AxiosInstance = axios.create({
    baseURL: 'http://192.168.100.29:8088',
    timeout: 3000,
});

/**
 * 请求拦截器
 */
fuel.interceptors.request.use((config: InternalAxiosRequestConfig) => {

    config.headers.set("hello", "android-veu")

    return config;

}, (err: any) => {
    return Promise.reject(err);

})



// 添加响应拦截器
fuel.interceptors.response.use((response: AxiosResponse) => {


    // 0 "" = false
    if (!response.data.code) {

        return response.data
    } else {


        //todo toast提示
        throw new ResponseError(response.data.code, response.data.message ?? "出现错误！")
    }

}, (err: Error) => {

    return Promise.reject(err);
})



export { fuel };